package Form;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import Control.CommonControls;
import Control.NeedyResident_CreateUser_Control;
import Control.User_CommonControls;
import Entity.NeedyResident_CreateAccount;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import java.util.Date;
import java.util.Calendar;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class NeedyResident_CreateUser_Form extends JPanel{
	private JTextField tf_nric;
	private JTextField tf_name;
	private JTextField tf_email;
	private JTextField tf_contactNo;
	private JTextField tf_address;
	private JTextField tf_monthlyIncome;
	private JTextField tf_familySize;
	private JButton btnNewButton;
	
	NeedyResident_CreateUser_Control controller;
	private JSpinner spinner;
	private JPasswordField pf_password;
	private JTextField tf_gender;
	
	public NeedyResident_CreateUser_Form() {
		controller = new NeedyResident_CreateUser_Control(this);
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JLabel lblNric = new JLabel("Nric");
		GridBagConstraints gbc_lblNric = new GridBagConstraints();
		gbc_lblNric.anchor = GridBagConstraints.EAST;
		gbc_lblNric.insets = new Insets(0, 0, 5, 5);
		gbc_lblNric.gridx = 0;
		gbc_lblNric.gridy = 1;
		add(lblNric, gbc_lblNric);
		
		tf_nric = new JTextField();
		GridBagConstraints gbc_tf_nric = new GridBagConstraints();
		gbc_tf_nric.insets = new Insets(0, 0, 5, 0);
		gbc_tf_nric.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_nric.gridx = 1;
		gbc_tf_nric.gridy = 1;
		add(tf_nric, gbc_tf_nric);
		tf_nric.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("Password");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 2;
		add(lblNewLabel, gbc_lblNewLabel);
		
		pf_password = new JPasswordField();
		GridBagConstraints gbc_pf_password = new GridBagConstraints();
		gbc_pf_password.insets = new Insets(0, 0, 5, 0);
		gbc_pf_password.fill = GridBagConstraints.HORIZONTAL;
		gbc_pf_password.gridx = 1;
		gbc_pf_password.gridy = 2;
		add(pf_password, gbc_pf_password);
		
		JLabel lblNewLabel_1 = new JLabel("Name");
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_1.gridx = 0;
		gbc_lblNewLabel_1.gridy = 3;
		add(lblNewLabel_1, gbc_lblNewLabel_1);
		
		tf_name = new JTextField();
		GridBagConstraints gbc_tf_name = new GridBagConstraints();
		gbc_tf_name.insets = new Insets(0, 0, 5, 0);
		gbc_tf_name.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_name.gridx = 1;
		gbc_tf_name.gridy = 3;
		add(tf_name, gbc_tf_name);
		tf_name.setColumns(10);
		
		JLabel lblNewLabel_5 = new JLabel("Address");
		GridBagConstraints gbc_lblNewLabel_5 = new GridBagConstraints();
		gbc_lblNewLabel_5.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_5.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_5.gridx = 0;
		gbc_lblNewLabel_5.gridy = 4;
		add(lblNewLabel_5, gbc_lblNewLabel_5);
		
		tf_address = new JTextField();
		GridBagConstraints gbc_tf_address = new GridBagConstraints();
		gbc_tf_address.insets = new Insets(0, 0, 5, 0);
		gbc_tf_address.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_address.gridx = 1;
		gbc_tf_address.gridy = 4;
		add(tf_address, gbc_tf_address);
		tf_address.setColumns(10);
		
		JLabel lblNewLabel_6 = new JLabel("Gender");
		GridBagConstraints gbc_lblNewLabel_6 = new GridBagConstraints();
		gbc_lblNewLabel_6.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_6.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_6.gridx = 0;
		gbc_lblNewLabel_6.gridy = 5;
		add(lblNewLabel_6, gbc_lblNewLabel_6);
		
		tf_gender = new JTextField();
		GridBagConstraints gbc_tf_gender = new GridBagConstraints();
		gbc_tf_gender.insets = new Insets(0, 0, 5, 0);
		gbc_tf_gender.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_gender.gridx = 1;
		gbc_tf_gender.gridy = 5;
		add(tf_gender, gbc_tf_gender);
		tf_gender.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel("Date of Birth");
		GridBagConstraints gbc_lblNewLabel_4 = new GridBagConstraints();
		gbc_lblNewLabel_4.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_4.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_4.gridx = 0;
		gbc_lblNewLabel_4.gridy = 6;
		add(lblNewLabel_4, gbc_lblNewLabel_4);
		
		setSpinner(new JSpinner());
		getSpinner().setModel(new SpinnerDateModel(CommonControls.minusYearToDate(CommonControls.getCurrentDate(), 1), CommonControls.minusYearToDate(CommonControls.getCurrentDate(), 1000), CommonControls.getCurrentDate(), Calendar.DAY_OF_YEAR));
		GridBagConstraints gbc_spinner = new GridBagConstraints();
		gbc_spinner.insets = new Insets(0, 0, 5, 0);
		gbc_spinner.gridx = 1;
		gbc_spinner.gridy = 6;
		add(getSpinner(), gbc_spinner);
		
		JLabel lblNewLabel_3 = new JLabel("Contact Number");
		GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints();
		gbc_lblNewLabel_3.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_3.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_3.gridx = 0;
		gbc_lblNewLabel_3.gridy = 7;
		add(lblNewLabel_3, gbc_lblNewLabel_3);
		
		tf_contactNo = new JTextField();
		GridBagConstraints gbc_tf_contactNo = new GridBagConstraints();
		gbc_tf_contactNo.insets = new Insets(0, 0, 5, 0);
		gbc_tf_contactNo.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_contactNo.gridx = 1;
		gbc_tf_contactNo.gridy = 7;
		add(tf_contactNo, gbc_tf_contactNo);
		tf_contactNo.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("Email");
		GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
		gbc_lblNewLabel_2.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_2.gridx = 0;
		gbc_lblNewLabel_2.gridy = 8;
		add(lblNewLabel_2, gbc_lblNewLabel_2);
		
		tf_email = new JTextField();
		GridBagConstraints gbc_tf_email = new GridBagConstraints();
		gbc_tf_email.insets = new Insets(0, 0, 5, 0);
		gbc_tf_email.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_email.gridx = 1;
		gbc_tf_email.gridy = 8;
		add(tf_email, gbc_tf_email);
		tf_email.setColumns(10);
		
		JLabel lblNewLabel_7 = new JLabel("Monthly Income");
		GridBagConstraints gbc_lblNewLabel_7 = new GridBagConstraints();
		gbc_lblNewLabel_7.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_7.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_7.gridx = 0;
		gbc_lblNewLabel_7.gridy = 9;
		add(lblNewLabel_7, gbc_lblNewLabel_7);
		
		tf_monthlyIncome = new JTextField();
		GridBagConstraints gbc_tf_monthlyIncome = new GridBagConstraints();
		gbc_tf_monthlyIncome.insets = new Insets(0, 0, 5, 0);
		gbc_tf_monthlyIncome.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_monthlyIncome.gridx = 1;
		gbc_tf_monthlyIncome.gridy = 9;
		add(tf_monthlyIncome, gbc_tf_monthlyIncome);
		tf_monthlyIncome.setColumns(10);
		
		JLabel lblNewLabel_8 = new JLabel("Family Size");
		GridBagConstraints gbc_lblNewLabel_8 = new GridBagConstraints();
		gbc_lblNewLabel_8.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_8.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_8.gridx = 0;
		gbc_lblNewLabel_8.gridy = 10;
		add(lblNewLabel_8, gbc_lblNewLabel_8);
		
		tf_familySize = new JTextField();
		GridBagConstraints gbc_tf_familySize = new GridBagConstraints();
		gbc_tf_familySize.insets = new Insets(0, 0, 5, 0);
		gbc_tf_familySize.fill = GridBagConstraints.HORIZONTAL;
		gbc_tf_familySize.gridx = 1;
		gbc_tf_familySize.gridy = 10;
		add(tf_familySize, gbc_tf_familySize);
		tf_familySize.setColumns(10);
		
		btnNewButton = new JButton("Create Account");
		final NeedyResident_CreateUser_Form panel = this;
		btnNewButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				if (CommonControls.validatePanelFields(panel) == true){
					if (User_CommonControls.CheckDuplicate(getTf_nric().getText()) == true){
						if (User_CommonControls.ValidateGender(getTf_gender().getText()) == true){
							if (User_CommonControls.ValidationContactNo(getTf_contactNo().getText()) == true){
								if (User_CommonControls.ValidationMonthlyIncome(Double.parseDouble(getTf_monthlyIncome().getText())) == true){
									if (User_CommonControls.ValidationFamilySize(Integer.parseInt(getTf_familySize().getText())) == true){
										controller.createAccount();
									}
									else
										JOptionPane.showMessageDialog(null, "Family Size is invalid (Between 0 to 20)");
									
								}
								else
									JOptionPane.showMessageDialog(null, "Monthly Income must be between 0 and 3000");
								
							}
							else
								JOptionPane.showMessageDialog(null, "Contact Number must be 8 digits long");
						}
						else
							JOptionPane.showMessageDialog(null, "For gender please enter either M of F");
					}
					else
						JOptionPane.showMessageDialog(null, "There is already an existing record with the same NRIC");
				}

			}
		});
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
		gbc_btnNewButton.gridx = 1;
		gbc_btnNewButton.gridy = 11;
		add(btnNewButton, gbc_btnNewButton);
	}

	public JTextField getTf_nric() {
		return tf_nric;
	}

	public JTextField getTf_gender() {
		return tf_gender;
	}

	public void setTf_gender(JTextField tf_gender) {
		this.tf_gender = tf_gender;
	}

	public JTextField getTf_monthlyIncome() {
		return tf_monthlyIncome;
	}

	public void setTf_monthlyIncome(JTextField tf_monthlyIncome) {
		this.tf_monthlyIncome = tf_monthlyIncome;
	}

	public JTextField getTf_familySize() {
		return tf_familySize;
	}

	public void setTf_familySize(JTextField tf_familySize) {
		this.tf_familySize = tf_familySize;
	}

	public void setTf_nric(JTextField tf_nric) {
		this.tf_nric = tf_nric;
	}

	public void setTf_password(JTextField tf_password) {
	}

	public JTextField getTf_name() {
		return tf_name;
	}

	public JTextField getTf_email() {
		return tf_email;
	}

	public void setTf_email(JTextField tf_email) {
		this.tf_email = tf_email;
	}

	public JTextField getTf_contactNo() {
		return tf_contactNo;
	}

	public void setTf_contactNo(JTextField tf_contactNo) {
		this.tf_contactNo = tf_contactNo;
	}

	public JTextField getTf_address() {
		return tf_address;
	}

	public void setTf_address(JTextField tf_address) {
		this.tf_address = tf_address;
	}

	public void setTf_name(JTextField tf_name) {
		this.tf_name = tf_name;
	}

	public JPasswordField getPf_password() {
		return pf_password;
	}

	public void setPf_password(JPasswordField pf_password) {
		this.pf_password = pf_password;
	}

	public JSpinner getSpinner() {
		return spinner;
	}

	public void setSpinner(JSpinner spinner) {
		this.spinner = spinner;
	}
}
